package com.story.modular.app.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.story.common.constant.factory.PageFactory;
import com.story.common.persistence.model.Search;
import com.story.common.persistence.model.VipType;
import com.story.core.base.controller.BaseController;
import com.story.core.log.LogObjectHolder;
import com.story.modular.app.dao.VipTypeMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Pyx
 * @PACKAGE_NAME : com.story.modular.app.controller
 * @FileName: VipTypeController
 * @date 2020/4/20 14:45
 */
@Controller
@RequestMapping("vipType")
@Api(value = "第二批 支付参数配置", tags = "第二批 支付参数配置")
public class VipTypeController extends BaseController {

    @Autowired
    private VipTypeMapper vipTypeMapper;
    private static String PREFIX = "/system/vipType/";

    @RequestMapping("index")
    public String index() {
        return PREFIX + "vipType.html";
    }

    @RequestMapping("/vipType_add")
    public String vipTypeAdd() {
        return PREFIX + "vipType_add.html";
    }

    @RequestMapping("/vipType_edit/{vipTypeId}")
    public String vipTypeUpdate(@PathVariable Integer vipTypeId, Model model) {
        VipType dict = vipTypeMapper.selectByPrimaryKey(vipTypeId);
        model.addAttribute("dict", dict);
        LogObjectHolder.me().set(dict);
        return PREFIX + "vipType_edit.html";
    }

    @ApiOperation("得到列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public Object list() {
        Page<VipType> page = new PageFactory<VipType>().defaultPage();
        List<Map<String, Object>> result = vipTypeMapper.getList(page);
        return result;
    }
    @ApiOperation("删除VIP类型")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public Object delete(String vipTypeId) {
        List<Integer> IdList = new ArrayList<Integer>();
        String[] IdArr = vipTypeId.split(",");
        for (String Id : IdArr) {
            vipTypeMapper.deleteByPrimaryKey(Integer.valueOf(Id));
        }
        return SUCCESS_TIP;
    }

    @ApiOperation("修改VIP类型")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object update(Integer id, String name, String price,Integer days) {
        VipType search = vipTypeMapper.selectByPrimaryKey(id);
        search.setName(name);
        search.setPrice(new BigDecimal(price));
        search.setDays(days);
        vipTypeMapper.updateByPrimaryKeySelective(search);
        return SUCCESS_TIP;
    }

    @ApiOperation("添加关键词")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ResponseBody
    public Object insert(String name, String price,Integer days) {
        Integer count = vipTypeMapper.selectByName(name);
        if (count == 0) {
            VipType vipType = new VipType();
            vipType.setPrice(new BigDecimal(price));
            vipType.setDays(days);
            vipType.setName(name);
            vipTypeMapper.insertSelective(vipType);
        } else {
            return "重复的内容";
        }
        return SUCCESS_TIP;
    }
    @ApiOperation("app列表")
    @RequestMapping(value = "/appList", method = RequestMethod.POST)
    @ResponseBody
    public Object appList() {
        return vipTypeMapper.appList();
    }
}
